package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.Workshop;
import com.efast.dmom.mes.dao.entity.WorkShopDao;
import com.efast.dmom.mes.datahandlers.base.PlantModelDataHandler;
import com.efast.dmom.mes.entity.WorkshopEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;
import com.efast.dmom.mes.util.TimeUtil;

@Component
public class WorkShopDataHandler extends PlantModelDataHandler<Workshop>{

	@Autowired
	private WorkShopDao workShopDao;
	
	@Override
	public ResultBean save(Workshop obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		WorkshopEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		BaseEntityDataUtil.setBaseEntityData(entity);	
	    int save = workShopDao.save(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		resultBean.setObject(obj);
		return resultBean;
	}
	
	@Override
	public ResultBean update(Workshop obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			workShopDao.update(obj.getInnerData());
		}
		return resultBean;
	}
	@Override
	public ResultBean delete(Workshop obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			workShopDao.delete(obj.getInnerData());
		}
		return resultBean;
	}

	public Workshop getById(long id) {
		Workshop workShop = new Workshop();
		workShop.setInnerData(workShopDao.getById(id));
		return workShop;
	}

	public Workshop getByName(String name) {
		Workshop workShop = new Workshop();
		workShop.setInnerData(workShopDao.getByName(name));
		return workShop;
	}

	public List<Workshop> getWorkshopByPlantId(long plantId) {
		List<WorkshopEntity> entityList = workShopDao.getWorkshopByPlantId(plantId);
		
		List<Workshop> workShopList = new ArrayList<Workshop>();
		if(entityList != null && entityList.size() > 0) {
		for (WorkshopEntity entityData:entityList) {
			Workshop workShop = new Workshop();
			workShop.setInnerData(entityData);
			workShopList.add(workShop);
		}
		return workShopList;
	  }
		return null;
	}

	public Workshop getByCode(String workshopCode) {
		Workshop workShop = new Workshop();
		workShop.setInnerData(workShopDao.getByCode(workshopCode));
		return workShop;
	}
	public List<Map<String,String>> getWorkshopCodeIdNameByPlantId(Long id) {
		return workShopDao.getWorkshopCodeIdNameByPlantId(id);
	}
	
	public int queryProductionLineById (Long id) {
		int queryProductionLineById = workShopDao.queryProductionLineById(id);
		return queryProductionLineById;
	}

	@Override
	public ResultBean saveBatch(List<Workshop> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Workshop> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Workshop> obj) {
		// TODO Auto-generated method stub
		return null;
	}


}
